<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\WechatController;
use App\Models\Prize;
use App\Service\Admin\PrizeService;
use Cache;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class PrizeController extends Controller
{
    const QRCODE = 'qrcode';
    private $prize;

    function __construct(PrizeService $prize)
    {
        // 自定义活动中间件
        $this->middleware('check.permission:prize');
        $this->prize = $prize;
    }

    /**
     * 奖品列表
     * @author 晚黎
     * @date   2016-10-29
     * @return [type]     [description]
     */
    public function index()
    {
        return view('admin.prize.index');
    }

    /**
     * datatables获取数据
     * @author 晚黎
     * @date   2016-10-29
     * @return [type]     [description]
     */
    public function ajaxIndex()
    {
        $responseData = $this->prize->getPrizesStat();
        return response()->json($responseData);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->activity->destroyActivity($id);
        return redirect('admin/prize');
    }


    /**
     * 上传type和文件，批量导入
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function batchStore(Request $request)
    {
        if ($this->prize->batchStorePrize($request)) {
            return response()->json(['status' => 0]);
        } else {
            return response()->json(['status' => 1], 500);
        }

    }

    /**
     * 门票列表
     */
    public function all()
    {
        return view('admin.prize.list');
    }

    /**
     * 门票列表
     */
    public function ajaxList()
    {
        $responseData = $this->prize->ajaxList();
        return response()->json($responseData);
    }

    /**
     * 异步修改线上线下设置
     * @param $aid
     * @param int $way
     * @return \Illuminate\Http\JsonResponse
     */
    public function ajaxSetting(Request $request)
    {
        $name = $request->input('name');
        if (empty($name)) {
            return response()->json(['status' => 1], 404);
        }
        $statusArr = json_decode(Cache::get(Prize::cacheStatusKey, '[]'),true);
        $statusArr[$name] = empty($statusArr[$name]) ? true : false;
        if ($statusArr[$name]) {
            $span = '<span class="status-span" style="color: blue">已启用</span>';
        } else {
            $span = '<span class="status-span">未启用</span>';
        }
        Cache::forever(Prize::cacheStatusKey, json_encode($statusArr));
        return response()->json(['status' => 0, 'data' => $span]);
    }

}
